/*
 *  shnodeuicolor_qt.cpp
 *  shaderz_v3
 *
 *  Created by Anjali on 5/5/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include <QColorDialog>

#include "shnode_color.h"
#include "shnodeuicolor_qt.h"

using namespace shaderz;
using namespace shaderzui::qt;

shNodeUI_color::shNodeUI_color( QWidget* InParent, shaderz::shNode* InNode )
	: ParentClass(InParent, InNode)
{
	// change the preview widet color to the color represented by the node
	shNodeColor* pNodeColor = dynamic_cast<shNodeColor*>(InNode);
	QColor nodeColor = Qt::darkGreen;
	if( pNodeColor )
	{
		int r = int(pNodeColor->red());
		int g = int(pNodeColor->green());
		int b = int(pNodeColor->blue());
		int a = int(pNodeColor->alpha());
		
		nodeColor = QColor( r, g, b, a);
	}
	
	mPreviewWidget->setPreviewType( nodePreviewWidget::SHPT_Color );
	mPreviewWidget->setSolidColor( nodeColor, false );
}

void shNodeUI_color::onPreviewWidgetClicked()
{
	ParentClass::onPreviewWidgetClicked();

	QColor currentColor( mPreviewWidget->getSolidColor() );
	QColor newColor = QColorDialog::getColor( currentColor, this );
	if( newColor != currentColor )
	{
		mPreviewWidget->setSolidColor( newColor, true );
		
		shNodeColor* pNodeColor = dynamic_cast<shNodeColor*>(node());
		if( pNodeColor )
		{
			// update the output cache
			pNodeColor->setRed( newColor.red() );
			pNodeColor->setGreen( newColor.green() );
			pNodeColor->setBlue( newColor.blue() );
			pNodeColor->setAlpha( newColor.alpha() );
		}
	}
}